package com.example.red_tea_house.moudle.Home.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.red_tea_house.MyApplication;
import com.example.red_tea_house.R;
import com.example.red_tea_house.entity.StoryBean;
import com.example.red_tea_house.moudle.Home.adapter.StoryAdapter;
import com.example.red_tea_house.moudle.Home.controller.getAndSetStoryController;
import com.example.red_tea_house.moudle.Read.view.readPageActivity;
import com.example.red_tea_house.moudle.Write.model.operateStory;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;


public class ReadFragment extends Fragment {
    private View rootview;

    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;
    private PagerSnapHelper snapHelper=new PagerSnapHelper();
    private LinearLayoutManager linearLayoutManager=new LinearLayoutManager(MyApplication.getContext());

    private List<StoryBean> data=new ArrayList<>();

    private getAndSetStoryController getAndSetStoryController =new getAndSetStoryController();

    private Context context=MyApplication.getContext();

    public ReadFragment() {

    }

    public static ReadFragment newInstance() {
        ReadFragment fragment = new ReadFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if(rootview==null){
            rootview=inflater.inflate(R.layout.read_page_layout,container,false);
        }
        initView();

        swipeRefreshLayout.setOnRefreshListener(()->{
            readStoryFromDB();
            refreshItems();
        });

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull @NotNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView,int newState){
                switch (newState){
                    case RecyclerView.SCROLL_STATE_IDLE://停止滚动
                        View view=snapHelper.findSnapView(linearLayoutManager);
                        RecyclerView.ViewHolder viewHolder=recyclerView.getChildViewHolder(view);
                        if(viewHolder!=null){

                        }
                        break;
                    case RecyclerView.SCROLL_STATE_DRAGGING:
                        break;
                    case RecyclerView.SCROLL_STATE_SETTLING:
                        break;
                }
            }
        });
        return rootview;
    }
    private void initView(){
        readStoryFromDB();
        swipeRefreshLayout=rootview.findViewById(R.id.readPageSwipeRefreshLayout);
        recyclerView=rootview.findViewById(R.id.readPageRecyclerView);
        refreshItems(); //因为内容要先刷新一下才会出现，所以初始化时先刷新一下
    }

    private void readStoryFromDB(){data= operateStory.loadLocalStory();}

    private void refreshItems(){
        getAndSetStoryController.clearOldRemoteStory();
        for(int i=0;i<10;i++){
            getAndSetStoryController.saveStoryFromRemote();
        }
        setLayout();
        if(swipeRefreshLayout.isRefreshing()){
            swipeRefreshLayout.setRefreshing(false);
        }
    }
    private void setLayout(){
        snapHelper.attachToRecyclerView(recyclerView);
        StoryAdapter storyAdapter=new StoryAdapter(data,rootview.getContext(),this);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(storyAdapter);
    }

    public void turnPage(){
        Intent intent=new Intent(context, readPageActivity.class);
        startActivity(intent);
    }
}